import { ref, getCurrentInstance } from 'vue'
import { useSubsetStore } from '@/stores/subset'
import { useUserStore } from '@/stores/user'
import { useCode } from '@/hooks/code'
import { addSubsetAPI, subsetAPI, deleteSubsetAPI, updateSubsetAPI, articleStateAPI } from '@/api'

const userStore = useUserStore()
const { tackleCode } = useCode()

const subsetStore = useSubsetStore()

export function useSubset(emits: any) {
  // 新建分组内容
  const inputValue = ref<number | string>()

  // 当前选择
  const selected = ref<number | string>('-1all')

  // 选择切换
  const changeOption = (id: number | string, type: string) => {
    if (id + type !== selected.value) {
      selected.value = id + type
      emits('nowSubset', { id, type })
    }
  }

  // 获取分组
  const rawSubset = async (e: number) => {
    const request = {
      token: userStore.token,
      classify: e,
    }
    const res: any = await subsetAPI(request)
    if (tackleCode(res.code)) {
      console.log(res)
      subsetStore.data = res.data.list
      subsetStore.count = res.data.count
    }
  }

  const proxy: any = getCurrentInstance()?.proxy
  // 取消
  function cancel() {
    inputValue.value = ''
  }
  // 新建分组
  async function confirm(e: number) {
    if (inputValue.value) {
      const request = {
        token: userStore.token,
        value: {
          moment: new Date(),
          name: inputValue.value,
          classify: e,
        },
      }

      const res: any = await addSubsetAPI(request)
      if (tackleCode(res.code)) {
        const sub = {
          id: res.data,
          name: inputValue.value!,
          value: 0,
        }
        subsetStore.data.push(sub)
        console.log(res)
        inputValue.value = ''
        proxy.$message({ type: 'primary', message: '插入成功' })
      }

      // subsetStore.data.push(obj)
    } else {
      proxy.$message({ type: 'error', message: '插入失败' })
    }
  }
  // 管理分组
  const visible = ref<boolean>(false)
  const showModal = () => {
    visible.value = true
  }
  // 删除分组
  const deleteSubset = async (e: number | string) => {
    // 提交后端处理
    const request = {
      token: userStore.token,
      subsetID: e,
    }
    const res: any = await deleteSubsetAPI(request)
    if (tackleCode(res.code)) {
      subsetStore.data = subsetStore.data.filter((obj: { value: any; id: number | string }) => {
        if (obj.id === e) {
          subsetStore.exclude.value += obj.value
        }
        return obj.id !== e
      })
      proxy.$message({ type: 'primary', message: '删除成功' })
    }
  }

  // 修改分组
  const updateSubset = async (e: any) => {
    // 提交后端处理
    const request = {
      token: userStore.token,
      subsetID: e.id,
      subsetName: e.name,
    }
    const res: any = await updateSubsetAPI(request)
    if (tackleCode(res.code)) {
      proxy.$message({ type: 'primary', message: '修改成功' })
    }
  }

  // 获取文章不同状态下的数量
  const state = ref<{ id: number; name: string; value: number }[]>([])
  const getState = async () => {
    const request = {
      token: userStore.token,
    }
    const res: any = await articleStateAPI(request)
    if (tackleCode(res.code)) {
      state.value = res.data
    }
    console.log('state', state)
  }

  return {
    inputValue,
    selected,
    changeOption,
    rawSubset,
    cancel,
    confirm,
    showModal,
    visible,
    deleteSubset,
    updateSubset,
    getState,
    state,
  }
}
